package cn.dnui_wnx725.dnuifood_16110100725.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.adapter.CollectFoodAdapter;
import cn.dnui_wnx725.dnuifood_16110100725.adapter.CollectShopAdapter;
import cn.dnui_wnx725.dnuifood_16110100725.bean.CollectListBean;
import cn.dnui_wnx725.dnuifood_16110100725.controller.FoodDetailActivity;
import cn.dnui_wnx725.dnuifood_16110100725.listener.ListListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.CollectListModel;

public class FavorFoodFragment extends BaseFragment {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    String flag="1";
    private SwipeRefreshLayout mSwipeLayout;


    ListListener<CollectListBean> listBeanListListener = new ListListener<CollectListBean>() {
        @Override
        public void onResponse(final List<CollectListBean> list) {
            final CollectFoodAdapter collectFoodAdapter = new CollectFoodAdapter(getActivity(),list,R.layout.item);
            CollectFoodAdapter.OnItemClickListener onItemClickListener = new CollectShopAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {
                    Intent intent =new Intent(getActivity(), FoodDetailActivity.class);
                    intent.putExtra("foodname",list.get(position).getFoodname());
                    intent.putExtra("foodimg",list.get(position).getPic());
                    intent.putExtra("foodid",list.get(position).getFood_id());
                    startActivity(intent);
                }
            };
            collectFoodAdapter.setList(list);
            collectFoodAdapter.setmOnItemClickListener(onItemClickListener);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.setAdapter(collectFoodAdapter);
            mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    collectFoodAdapter.setList(list);
                    collectFoodAdapter.notifyDataSetChanged();
                    mSwipeLayout.setRefreshing(false);
                }
            });
        }

        @Override
        public void onFail(String msg) {

        }
    };


    @Override
    void initView() {
        recyclerView=view.findViewById(R.id.rv_favor_food);
        mSwipeLayout = view.findViewById(R.id.id_swipe_ly);
        mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
    }

    @Override
    void initEvent() {
        CollectListModel collectListModel = new CollectListModel();
        collectListModel.getCollectList(getUserID(),flag,listBeanListListener);
    }

    @Override
    void initData() {


    }

    public FavorFoodFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        setLayout_file(R.layout.fragment_favor_food);
        return super.onCreateView(inflater,container,savedInstanceState);
    }

    private void request_list(){
        CollectListModel collectListModel = new CollectListModel();
        collectListModel.getCollectList(getUserID(),"1",listBeanListListener);
    }

    @Override
    public void onResume() {
        super.onResume();
        Toast.makeText(getActivity(),getUserID(),Toast.LENGTH_SHORT).show();
        request_list();
    }

}
